package com.example.demo;

import java.io.*;
import java.net.*;

public class StaticFileServer1 {
    private static final int PORT = 8081;
    private static final String WEB_ROOT2 = "webroot"; // 替换为实际的静态文件目录
    private static final String WEB_ROOT = "C:\\Users\\32398\\Desktop\\openapi2\\"; // 替换为实际的静态文件目录

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(PORT);
        System.out.println("Server started on port " + PORT);

        while (true) {
            Socket socket = serverSocket.accept();
            new Thread(new RequestHandler(socket)).start();
        }
    }

    private static class RequestHandler implements Runnable {
        private Socket socket;

        public RequestHandler(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            try {
                InputStream inputStream = socket.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                String line = reader.readLine();
                String filePath = getFilePath(line);

                if (filePath != null) {
                    File file = new File(WEB_ROOT + filePath);
                    if (file.exists()) {
                        OutputStream outputStream = socket.getOutputStream();
                        outputStream.write("HTTP/1.1 200 OK".getBytes());
                        outputStream.write("Content-Type: ".getBytes());
                        outputStream.write(getContentType(file).getBytes());
                        outputStream.write("".getBytes());
                        outputStream.flush();
                        FileInputStream fileInputStream = new FileInputStream(file);
                        byte[] buffer = new byte[4096];
                        int bytesRead;
                        while ((bytesRead = fileInputStream.read(buffer)) != -1) {
                            outputStream.write(buffer, 0, bytesRead);
                        }
                        fileInputStream.close();
                    } else {
                        sendErrorResponse(socket, "404 Not Found");
                    }
                } else {
                    sendErrorResponse(socket, "400 Bad Request");
                }

                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        private String getFilePath(String requestLine) {
            if (requestLine != null && requestLine.startsWith("GET")) {
                String[] parts = requestLine.split(" ");
                if (parts.length > 1) {
                    return parts[1];
                }
            }
            return null;
        }

        private String getContentType(File file) {
            String fileName = file.getName();
            String contentType = "text/html"; // 默认为HTML文件类型
            if (fileName.endsWith(".css")) {
                contentType = "text/css";
            } else if (fileName.endsWith(".js")) {
                contentType = "application/javascript";
            } else if (fileName.endsWith(".jpg") || fileName.endsWith(".jpeg")) {
                contentType = "image/jpeg";
            } else if (fileName.endsWith(".png")) {
                contentType = "image/png";
            } else if (fileName.endsWith(".gif")) {
                contentType = "image/gif";
            }
            return contentType;
        }

        private void sendErrorResponse(Socket socket, String errorMessage) throws IOException {
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write("HTTP/1.1 ".getBytes());
            outputStream.write(errorMessage.getBytes());
            outputStream.write("".getBytes());
            outputStream.flush();
        }
    }
}
